package no.group.gowther.service.dto.param;

import no.group.gowther.enums.FileType;
import no.group.gowther.exception.BusinessException;
import no.group.gowther.exception.ErrorMessage;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

public class FileQueryListDTO {
    private final Optional<Path> dirPath;
    private final Optional<FileType> fileType;
    private final Optional<Page> page;

    public FileQueryListDTO(Path dirPath, FileType fileType, Page page) {
        this.dirPath = Optional.ofNullable(dirPath);
        this.fileType = Optional.ofNullable(fileType);
        this.page = Optional.ofNullable(page);
        check();
    }

    private void check() {
        if (dirPath.isPresent() && (!Files.isDirectory(dirPath.get()) || !Files.exists(dirPath.get()))) {
            throw BusinessException.createException(ErrorMessage.PARAM_ERROR.getErrorCode(), "[%s]不是目录或该目录不存在".formatted(dirPath.get()));
        }
    }

    public Optional<Path> getDirPath() {
        return dirPath;
    }

    public Optional<FileType> getFileType() {
        return fileType;
    }

    public Optional<Page> getPage() {
        return page;
    }
}
